<template>
	<view>
		<view class="goods-list">
			<view v-for="(goods,i) in goodsList" :key="i" @click="gotoDetail(goods)">
				<Mygoods :goods="goods"></Mygoods>	
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				queryObj:{
					query:'',
					cid:'',
					//首页，第几页
					pagenum:1,
					//每页多少条数据
					pagesize:10,
				},
				// 商品数据
				goodsList:[],
				//数据总和
				total:0,
				// 是否正在请求数据
				isloading: false
			};
		},
		onLoad(options) {
			this.queryObj.query = options.query || ''
			this.queryObj.cid = options.cid || ''
			//调用获取商品列表数据的方法
			this.getGoodsList()
		},
		methods:{
			async getGoodsList(cb){
				 //  打开节流阀
				this.isloading = true
				const {data:res} = await uni.$http.get('/api/public/v1/goods/search',this.queryObj)
				 //  关闭节流阀
				this.isloading = false
				// 只要数据请求完毕，就立即按需调用 cb 回调函数
				  cb && cb()
				if(res.meta.status !== 200) return uni.$showMsg()
				this.goodsList = [...this.goodsList,...res.message.goods]
				this.total = res.message.total
			},
			gotoDetail(goods){
				uni.navigateTo({
					url:'/subpkg/goods_detail/goods_detail?goods_id=' + goods.goods_id
				})
				console.log(goods)
			}
		},
		onReachBottom() {
			if(this.queryObj.pagenum * this.queryObj.pagesize >= this.total) return uni.$showMsg('数据加载完毕！')
			// 判断是否正在发起请求，如果是则开启节流阀，不发额外的请求
			if(this.isloading) return
			//让页码自增加 1
			this.queryObj.pagenum += 1
			// 重新获取数据
			this.getGoodsList()
		},
		onPullDownRefresh() {
			// 重置数据
			this.queryObj.pagenum = 1
			this.total = 0
			this.isloading = false
			this.goodsList = []
			// 重新发起请求(stopPullDownRefresh关闭下拉刷新)
			this.getGoodsList(()=> uni.stopPullDownRefresh())
		}
	}
</script>

<style lang="scss">
</style>
